﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Linq;

namespace XEvents.Library.Targets
{
    public class Target_Bucketizer : TargetData
    {
        private Target_Bucketizer(SqlServer server, EventSessionTarget parent)
            : base(server, parent)
        { }

        protected override void LoadData()
        { }

        protected override void InitalizeTarget()
        {
            //throw new NotImplementedException();
        }

        /*
        <CounterTarget truncated="0">
          <Packages>
            <Package name="package0" />
            <Package name="sqlos" />
            <Package name="sqlserver">
              <Event name="sql_statement_starting" count="692" />
              <Event name="sql_statement_completed" count="691" />
            </Package>
            <Package name="SecAudit" />
          </Packages>
        </CounterTarget>
        */

        /*
        <BucketizerTarget truncated="0" buckets="256">
          <Slot count="2715303" trunc="0">
            <value>14</value>
          </Slot>
        </BucketizerTarget>
        */

        public System.Data.DataTable GetEventsAsDataTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable("EventInformation");

            dt.Columns.Add("Count");
            dt.Columns.Add("Truncated");
            dt.Columns.Add("BucketSlot");

            
            foreach (Bucket b in BucketSlots)
            {
                System.Data.DataRow dr = dt.Rows.Add();
                dr["Count"] = b.Count;
                dr["Truncated"] = b.Truncated;
                dr["BucketSlot"] = b.BucketSlot;
            }
            return dt;
        }

        internal static TargetData GetTargetData(string data, SqlServer server, EventSessionTarget parent)
        {
            XDocument targetdata = XDocument.Parse(data);

            return (from e in targetdata.Elements()
                    select new Target_Bucketizer(server, parent)
                    {
                        truncated = (int)e.Attribute("truncated"),
                        bucketslots = new List<Bucket>(
                            from d in e.Elements()
                            select new Bucket
                            {
                                count = (int)d.Attribute("count"),
                                truncated = (int)d.Attribute("trunc"),
                                bucket = (string)d.Element("value").Value
                            })
                    }).ElementAt(0);
        }

        private int truncated;
        private int buckets;

        private List<Bucket> bucketslots;

        public List<Bucket> BucketSlots
        {
            get { return bucketslots; }
        }

        public int Buckets
        {
            get { return buckets; }
        }
        public int Truncated
        {
            get { return truncated; }
        }
    }
}
